package com.open.party.cloud.view.home.jiCengDangJian

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.OrgDeptBean
import com.open.party.cloud.model.PartyOrgVo
import com.open.party.cloud.view.home.jiCengDangJian.org.adapter.OrgForCityCommListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class OrgForCityCommMainListActivity : AppEventBusRecycleViewTitleBaseActivity<PartyOrgVo>(), OnSuperListener<OrgDeptBean> {

    var viewModel: BizViewModel? = null
    var orgId = "697742463212716289"
    var orgName = "六盘水市委组织部"

    private var adapter: OrgForCityCommListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle(orgName)

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = OrgForCityCommListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<PartyOrgVo>) {
        val pageReq = PageReq<PartyOrgVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = 500

        val vo = PartyOrgVo()
        vo.orgId = orgId
        vo.isdelete = false
        pageReq.data = vo
        viewModel!!.getOrgForCityCommListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<OrgDeptBean>>) {
        if (result.eventType != "getOrgForCityCommListData") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<OrgDeptBean>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: OrgDeptBean, flag: String?) {
        XUtils.intent().openActivity(this@OrgForCityCommMainListActivity, OrgForCityCommMemberListActivity::class.java)
            .putStringExtra("deptId", itemData.id)
            .putStringExtra("deptName", itemData.name)
            .start()
    }
}